/**
 *====================================================
 * 文件名称: ContainerInventoryHistoryServiceImpl.java
 * 修订记录：
 * No    日期				作者(操作:具体内容)
 * 1.    2016年5月24日			chenxy(创建:创建文件)
 *====================================================
 * 类描述：(说明未实现或其它不应生成javadoc的内容)
 */
package com.yuexunit.finance.project.service;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;

import com.yuexunit.finance.common.util.BeanUtils;
import com.yuexunit.finance.project.entity.ContainerInventoryHistoryEntity;
import com.yuexunit.finance.project.entity.lexicon.AbnormalStateEnum;
import com.yuexunit.finance.project.entity.lexicon.AbnormalTypeEnum;
import com.yuexunit.inject.annotation.Implements;
import com.yuexunit.persist.PersistUtil;
import com.yuexunit.util.StringUtil;

/**
 * @ClassName: ContainerInventoryHistoryServiceImpl
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @author chenxy
 * @date 2016年5月24日 下午5:06:01
 */
@Implements(ContainerInventoryHistoryService.class)
public class ContainerInventoryHistoryServiceImpl implements ContainerInventoryHistoryService {

	@Override
	public Long createContainerInventory(ContainerInventoryHistoryEntity entity) {
		if (entity.getAbnormal() == null) {
			entity.setAbnormal(Boolean.TRUE);
		}
		if (entity.getAbnormalState() == null) {
			entity.setAbnormalState(AbnormalStateEnum.UNKNOWN);
		}
		if (entity.getAbnormal().booleanValue()) {
			entity.setAbnormalType(AbnormalTypeEnum.UNKNOWN);
		} else {
			entity.setAbnormalType(AbnormalTypeEnum.TALLY);
		}
		if (StringUtils.isNotBlank(entity.getCaseNumber())) {
			entity.setCaseNumber(entity.getCaseNumber().toUpperCase());
		}
		if (entity.getTallyingState() == null) {
			entity.setTallyingState(Boolean.FALSE);
		}
		PersistUtil.insert(entity);
		return entity.getInventoryHistoryId();
	}

	@Override
	public void updateContainerInventory(ContainerInventoryHistoryEntity entity) {
		ContainerInventoryHistoryEntity persist = inquireContainerInventory(entity.getInventoryHistoryId());
		BeanUtils.copyProperties(entity, persist);
		if (StringUtils.isNotBlank(persist.getCaseNumber())) {
			persist.setCaseNumber(persist.getCaseNumber().toUpperCase());
		}
		PersistUtil.update(persist);
	}

	@Override
	public void renovateContainerInventory(ContainerInventoryHistoryEntity persist) {
		PersistUtil.update(persist);
	}

	@Override
	public void deleteContainerInventory(Long inventoryHistoryId) {
		PersistUtil.delete(ContainerInventoryHistoryEntity.class, inventoryHistoryId);
	}

	@Override
	public ContainerInventoryHistoryEntity inquireContainerInventory(Long inventoryHistoryId) {
		return PersistUtil.get(ContainerInventoryHistoryEntity.class, inventoryHistoryId);
	}

	@Override
	public List<ContainerInventoryHistoryEntity> inquireTallyingContainerInventoryList(Long tallyingId) {
		DetachedCriteria criteria = DetachedCriteria.forClass(ContainerInventoryHistoryEntity.class);
		criteria.add(Restrictions.eq("tallyingId", tallyingId));
		return PersistUtil.get(criteria);
	}

	@Override
	public List<ContainerInventoryHistoryEntity> inquireRecieveNoticeContainerInventory(String waybillNumber, String caseNumber) {
		DetachedCriteria criteria = DetachedCriteria.forClass(ContainerInventoryHistoryEntity.class);
		criteria.add(Restrictions.isNotNull("financeId"));
		criteria.add(Restrictions.eq("waybillNumber", waybillNumber));
		criteria.add(Restrictions.eq("caseNumber", caseNumber));
		return PersistUtil.get(criteria);
	}

	@Override
	public List<ContainerInventoryHistoryEntity> inquireLogisticsContainerInventoryList(String waybillNumber, String caseNumber) {
		DetachedCriteria criteria = DetachedCriteria.forClass(ContainerInventoryHistoryEntity.class);
		criteria.add(Restrictions.isNull("financeId"));
		criteria.add(Restrictions.eq("waybillNumber", waybillNumber));
		criteria.add(Restrictions.eq("caseNumber", caseNumber));
		return PersistUtil.get(criteria);
	}

	@Override
	public List<ContainerInventoryHistoryEntity> inquireEmptyFinanceContainerInventory(String waybillNumber, String caseNumber) {
		DetachedCriteria criteria = DetachedCriteria.forClass(ContainerInventoryHistoryEntity.class);
		criteria.add(Restrictions.isNull("financeId"));
		criteria.add(Restrictions.eq("waybillNumber", waybillNumber));
		if (StringUtils.isNotBlank(caseNumber)) {
			criteria.add(Restrictions.eq("caseNumber", caseNumber));
		}
		return PersistUtil.get(criteria);
	}

	@Override
	public List<Long> inquireTallyingIdsFromContainerInventory(String waybillNumber, String caseNumber) {
		DetachedCriteria criteria = DetachedCriteria.forClass(ContainerInventoryHistoryEntity.class);
		if (StringUtil.isNotBlank(waybillNumber)) {
			criteria.add(Restrictions.like("waybillNumber", waybillNumber));
		}
		
		if (StringUtil.isNotBlank(caseNumber)) {
			criteria.add(Restrictions.like("caseNumber", "%" + caseNumber + "%"));
		}
		
		criteria.setProjection(Projections.projectionList().add(Projections.groupProperty("tallyingId")));
		return PersistUtil.get(criteria);
	}

	@Override
	public List<ContainerInventoryHistoryEntity> inquireContainerInventorys(String waybillNumber, String caseNumber) {
		DetachedCriteria criteria = DetachedCriteria.forClass(ContainerInventoryHistoryEntity.class);
		criteria.add(Restrictions.eq("waybillNumber", waybillNumber));
		criteria.add(Restrictions.eq("caseNumber", caseNumber));
		return PersistUtil.get(criteria);
	}
}
